package dog

type IDogLastWord interface {
	SetCmd(cmd string)
	Execute()
}

type LastWordFactory struct {
	mapping map[string]LastWordCreater
}

var lastword_factory *LastWordFactory = &LastWordFactory{
	mapping: make(map[string]LastWordCreater),
}

func GetDefaultLastWordFactory() *LastWordFactory {
	return lastword_factory
}

type LastWordCreater interface {
	Create(cmd string) IDogLastWord
}

type LastWordCreaterFunc struct {
	creater_func func(string) IDogLastWord
}

func (this *LastWordCreaterFunc) Create(cmd string) IDogLastWord {
	return this.creater_func(cmd)
}

func NewLastWordCreater(creater_func func(string) IDogLastWord) LastWordCreater {
	return &LastWordCreaterFunc{creater_func: creater_func}
}

func (this *LastWordFactory) CreateLastWord(lwname string, cmd string) IDogLastWord {
	creater := this.mapping[lwname]
	if creater != nil {
		return creater.Create(cmd)
	}
	return nil
}

func (this *LastWordFactory) GetAllLastWordName() []string {
	var names []string

	for k, _ := range this.mapping {
		names = append(names, k)
	}

	return names
}

func (this *LastWordFactory) Init() {
	this.mapping["SayHi"] = NewLastWordCreater(func(s string) IDogLastWord {
		return &LastWordSayHi{
			name: s,
		}
	})

	this.mapping["CMD"] = NewLastWordCreater(func(s string) IDogLastWord {
		return &LastWordCMD{
			cmd: s,
		}
	})

	this.mapping["HttpGet"] = NewLastWordCreater(func(s string) IDogLastWord {
		return &LastWordHttpGet{
			url: s,
		}
	})
}
